import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * GUI class
 *
 */
public class View extends JFrame implements Observer {

	private JList list;
	
	private Model model;
	private Controller controller;
	
	private JButton failedButton;
	private JComboBox recipeSelector;
	private JLabel infoLabel;
	
	public View(Model model) {
		this.model = model;
		this.controller = new Controller(this);
		model.addObserver(this);
		
		/* Set JFrame attributes */
		setTitle("I, Chef");
		
		setLayout(new BorderLayout());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setPreferredSize(new Dimension(1000, 500));
		
		//Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		//setPreferredSize(dim);
		
		/* Create button panel*/
		FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
		JPanel topPanel = new JPanel(layout);
		
		// create plan button
		JButton planButton = new JButton("Plan!");
		planButton.setActionCommand("plan");
		planButton.addActionListener(controller);
		topPanel.add(planButton);
		
		// create failed button
		failedButton = new JButton("Step failed");
		failedButton.setActionCommand("failed");
		failedButton.addActionListener(controller);
		failedButton.setEnabled(false);
		topPanel.add(failedButton);
		
		// create recipe selector
		Set<String> keySet = model.getRecipes().keySet();
		recipeSelector = new JComboBox(keySet.toArray()); 
		topPanel.add(recipeSelector);
		
		// create info label
		infoLabel = new JLabel();
		topPanel.add(infoLabel);
		
		add(topPanel, BorderLayout.NORTH);
		
		/* Create text area */
		list = new JList();
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.addListSelectionListener(controller);
		
		JScrollPane scrollPane = new JScrollPane(list);
		
		add(scrollPane, BorderLayout.CENTER);    
		
		pack();
	}
	
	//@Override
	public void update(Observable o, Object arg) {
		Plan p = model.getPlan();
		list.setListData(model.getActions());
		
		
	}
	
	private class Controller implements ActionListener, ListSelectionListener {
		
		private View view;
		
		public Controller(View view) {
			this.view = view;
		}

		//@Override
		public void valueChanged(ListSelectionEvent arg0) {
			if (!list.getValueIsAdjusting()) {
				failedButton.setEnabled(!list.isSelectionEmpty());
			}
		}

		public void actionPerformed(ActionEvent e) {
			String cmd = e.getActionCommand();

			String recipe = (String) recipeSelector.getSelectedItem();

			try {
				if(cmd.equals("plan")){
						
					model.createPlan(recipe);
					infoLabel.setText("Plan created! Follow the steps below to cook your food.");

				} else if (cmd.equals("failed")) {

					Action failedAction = (Action) list.getSelectedValue();
					model.actionFailed(failedAction, recipe);

					infoLabel.setText("Created a recovery plan! Continue from the beginning to complete your meal.");
				}

			} catch (PlanningFailedException ex) {
				infoLabel.setText("");
				
				JOptionPane.showMessageDialog(view,
					    ex.getMessage(),
					    "Error",
					    JOptionPane.ERROR_MESSAGE);
			}

		}
		
	}
	

}
